
package com.yimixian.app.mvp.basehttp;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.util.UiUtils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public abstract class BaseHttpMvpFragment extends Fragment implements BaseHttpMvpView {
    private Dialog mDialog;
    private PopupWindow mPopupWindow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    /**
     * 显示加载框
     * 
     * @param msg 提示语
     */
    public void showLoadingDialog(String msg, boolean needToScreenFull) {
        if (!isAdded())
            return;
        if (mDialog == null) {
            mDialog = createLoadingDialog(getActivity(), msg, needToScreenFull);
        }
        mDialog.show();
//        UiUtils.showAlertDialog(mDialog);
    }

    public void showTransDialog(String msg, boolean needToScreenFull) {
        if (!isAdded())
            return;
        if (mDialog == null) {
            mDialog = createLoadingDialog(getActivity(), msg, needToScreenFull);
        }
        v.setBackgroundColor(Color.TRANSPARENT);
//        UiUtils.showAlertDialog(mDialog);
        mDialog.show();
    }

    /**
     * 隐藏加载框
     */
    public void hideDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    private View v;

    /**
     * 得到自定义的progressDialog
     * 
     * @param context
     * @param msg
     * @return
     */
    public Dialog createLoadingDialog(Context context, String msg, boolean needToScreenFull) {

        LayoutInflater inflater = LayoutInflater.from(context);
        v = inflater.inflate(R.layout.progress_dialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.loading_view);// 加载布局
        // main.xml中的ImageView
        TextView tipTextView = (TextView) v.findViewById(R.id.loading_text);// 提示文字
        // 加载动画
        if (!StringUtils.isEmpty(msg)) {
            tipTextView.setVisibility(View.VISIBLE);
            tipTextView.setText(msg);// 设置加载信息
        } else {
            tipTextView.setVisibility(View.GONE);
        }

        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
        if (needToScreenFull) {
            Window win = loadingDialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;
            win.setAttributes(lp);
        }

        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        return loadingDialog;
    }

    public void showPopupWindow(Context mContext, int id) {
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(mContext).inflate(
                R.layout.pop_window, null);
        if (mPopupWindow == null) {
            mPopupWindow = new PopupWindow(contentView,
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true) {
            };
        }
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        mPopupWindow.setBackgroundDrawable(dw);
        mPopupWindow.setOutsideTouchable(false);
        // 设置好参数之后再show
        if (getActivity().findViewById(id) != null) {
            mPopupWindow.showAtLocation(getActivity().findViewById(id), Gravity.CENTER
                    | Gravity.CENTER, 0, 0);
        }
    }

    public void hidePopupWindow() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }
    }

    @Override
    public void showLoadingDataUI() {

    }

    @Override
    public void hideLoadingDataUI() {
        
    }

    @Override
    public void showHttpSucUI() {

    }

    @Override
    public void showHttpFailUI(String failInfo) {
        UiUtils.showToast(failInfo);
    }

    @Override
    public void showNetWorkUnavailableUI() {
        UiUtils.showToast(SystemFramework.getInstance().getGlobalContext()
                .getString(R.string.errcode_network_unavailable));
    }

    @Override
    public void onLoadMoreComplete() {
        
    }

    @Override
    public void onRefreshComplete() {
        
    }

    @Override
    public void showNoDataUI() {
        
    }

    @Override
    public void hideNoDataUI() {
        
    }
}

